using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/* =====================================================================

  File:      OrderList.aspx for Adventure Works Cycles Storefront Sample
  Summary:   Lists a brief summary of all orders for the authenticated user
  Date:	     June 16, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

namespace Microsoft.Samples.SqlServer {

    public partial class OrderList : System.Web.UI.Page
    {

    
//         public OrderList() {
//             Page.Init += new System.EventHandler(Page_Init);
//         }

        //*******************************************************
        //
        // The Page_Load event on this page is used to obtain
        // from a database a collection of all orders placed
        // by the current customer, and the customer account details.  
		// The collection of orders is then
        // databound to a templated asp:datalist control.
        //
        //*******************************************************

        private void Page_Load(object sender, System.EventArgs e) {

            String customerID = User.Identity.Name;

            // Obtain and bind a list of all orders ever placed by visiting customer
            OrdersDB orderHistory = new OrdersDB();
        
            MyList.DataSource = orderHistory.GetCustomerOrders(customerID);
            MyList.DataBind();

            // Hide the list and display a message if no orders have ever been made
            if (MyList.Items.Count == 0) {
                MyError.Text = "You have no orders to display.";
                MyList.Visible = false;
            }
        }

        private void Page_Init(object sender, EventArgs e) {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
        }

		#region Web Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
		}

		/// <summary>
		///		Event handler for when the user clicks on the submit button.  This updates the customer 
		///		information (first name, last name, email address, password, subscription status)
		///		based on the information specified by the user.
		/// </summary>
		/// <param name="sender">Where the event came from</param>
		/// <param name="e">Information about the event</param>
		protected void UpdateButton_Click(object sender, ImageClickEventArgs e)
		{
			CustomersDB customers = new CustomersDB();

			String passwordHash = CustomersDB.CreatePasswordHash(SettingsControl.OldPasswordText,
											   (String)Page.Session["PWSalt"]);
			if (!(passwordHash.Equals(Page.Session["PWHash"])))
			{
				SettingsControl.ErrorMessage = "The old password you entered does not match.";
				return;
			}


			if (SettingsControl.PasswordText.Equals(SettingsControl.ConfirmPasswordText))
			{

				customers.UpdateCustomer(int.Parse(Context.User.Identity.Name),
										 SettingsControl.FirstNameText,
										 SettingsControl.LastNameText,
										 SettingsControl.EmailText,
										 SettingsControl.PasswordText,
										 (String)Page.Session["PWSalt"],
					(SettingsControl.IsEmailPromotionChecked) ? 1 : 0);
				if (!(SettingsControl.PasswordText.Equals(String.Empty)))
					Page.Session["PWHash"] = CustomersDB.CreatePasswordHash(
						SettingsControl.PasswordText, (String)Page.Session["PWSalt"]);
				SettingsControl.ErrorMessage = "";
				SettingsControl.OnAccountSettingsUpdate();
			}
			else
			{
				SettingsControl.ErrorMessage = "The new password does not match the confirmation password.";
			}
		}

		#endregion

    }
}
